﻿/*
 * BindAjaxPost
 */
!function ($) {
    $.fn.BindCountDown = function (opts) {
        var defaults = {
            step: 1,
            total: 60,
            intervalPerStep: 1000,
            text: '秒后可重新操作'
        }
        var options = $.extend(defaults, opts);
        var $btn = $(this);

        var isInput = $btn.is("input");

        var originalVal = isInput ? $btn.val() : $btn.text();

        var interval = setInterval(function () {
            $btn.button('loading');
            options.total -= options.step;
            if (options.total == 0) {
                clearInterval(interval);
                if (isInput)
                    $btn.val(originalVal);
                else
                    $btn.text(originalVal);
                $btn.button('reset');
                return;
            }
            if (isInput)
                $btn.val(options.total + options.text);
            else
                $btn.text(options.total + options.text);
        }, options.intervalPerStep * options.step);

    }
    $.fn.BindAjaxPost = function (opts) {


        var defaults = {
            form: $(this).parents("form:first"),
            action: $(this).parents("form:first").attr("action"),
            box: $("#MessageBox"),
            boxTitle: "提示信息",
            dialogType: "modal",
        };
        var options = $.extend(defaults, opts);
        var form = options.form;
        $(this).click(function () {
            var btn = $(this);
            $(btn).button('loading');
            var data = $(form).serialize();
            $.post(options.action, $(form).serialize(), function (re) {
                if (re.Result) {
                    if (options.dialogType != "modal") {
                        $(options.box).html(re.Message).dialog({
                            modal: true,
                            title: options.boxTitle,
                            position: { my: "center", at: "center", of: $(form) },
                            show: {
                                effect: "blind",
                                duration: 500
                            },
                            resizable: false
                        });
                    }
                    else {
                        $(options.box).find(".modal-title").html(options.boxTitle);
                        $(options.box).find(".modal-body").html(re.Message);
                        $(options.box).modal('show');
                    }
                    if (!!re.Url) {
                        if (re.Url == 'none') {
                            if ($(btn).hasClass("hascountdown")) {
                                $(btn).BindCountDown({});
                            }
                            return;
                        }
                        else
                            setTimeout(function () { top.location = re.Url; }, 1000);
                    }
                    else
                        setTimeout(function () { top.location = self.location; }, 1000);
                }
                else {
                    if (options.dialogType != "modal") {
                        $(options.box).html(re.Message).dialog({
                            modal: true,
                            title: options.boxTitle,
                            position: { my: "center", at: "center", of: $(form) },
                            buttons: [
                                {
                                    text: '确定', click: function () {
                                        $(this).dialog("close");
                                    }
                                }
                            ],
                            show: {
                                effect: "blind",
                                duration: 500
                            },
                            resizable: false
                        });
                    }
                    else {
                        $(options.box).find(".modal-title").html(options.boxTitle);
                        $(options.box).find(".modal-body").html(re.Message);
                        $(options.box).modal('show');
                    }
                    if (!!re.Url) {
                        setTimeout(function () { top.location = re.Url; }, 3000);
                    }
                }
                $(btn).button("reset");
            }).error(function () {
                $(btn).button("reset");
            });
            return false;
        });
    }
    $.ProcessReturnErrors = function (opts) {
        var defaults = {
            box: $("#MessageBox"),
            title: "提示",
            message: "过程中出现错误!",
            url: undefined
        }
        var options = $.extend(defaults, opts);

        $(options.box).html(opts.message).dialog({
            modal: true,
            title: options.title,
            resizable: false,
            buttons: [{
                text: "确定",
                click: function () {
                    $(options.box).dialog("close");
                    if (opts.url != undefined)
                        top.location = opts.url;
                }
            }]
        });
        setTimeout(function () {
            $(options.box).dialog("close");
            if (opts.url != undefined)
                top.location = opts.url;
        }
                , 1000);
    };
}(jQuery);
/*
 * 处理Ajax错误！
 */
!function ($) {
    $(document).ajaxError(function (event, jqxhr, settings, exception) {
        //console.log(jqxhr.status);
        if (jqxhr.status == 401) {
            $.ProcessReturnErrors({ message: "请先登陆！", url: "/Admin/Home/Logon" }); // 需要修改Url的设置，此处不应使用Hard Code
        }
    });

    $(document).ajaxComplete(function (event, xhr, options) {
        //console.log(xhr.getAllResponseHeaders());
        //console.log(xhr.status);
        //if (!!xhr.getResponseHeader('location') && xhr.status == 200)
        //    top.location = xhr.getResponseHeader('location');
    });
}(jQuery);
/**
 * 页面提交绑定
*/
!function ($) {
    $("#btnSave").BindAjaxPost({ box: "#ModalMessageBox" });
    $("#btnLogin").BindAjaxPost({ box: "#ModalMessageBox" });
    $("#btnRegister").BindAjaxPost({ box: "#ModalMessageBox" });
}(window.jQuery);